package com.pansoft.openplanet.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.efounder.utils.JfResourceUtil;
import com.efounder.utils.ResStringUtil;
import com.pansoft.kline.activity.TransactionTrendActivity;
import com.pansoft.kline.util.BtcKanNumberFormatUtil;
import com.pansoft.kline.util.KLineUtil;
import com.pansoft.kline.util.MarketNumberUtils;
import com.pansoft.kline.util.SelfSelectUtils;
import com.pansoft.openplanet.R;
import com.pansoft.openplanet.bean.BtckanDetailBean;
import com.pansoft.openplanet.manager.CurrencyAndBourseManager;

import java.math.BigDecimal;
import java.util.List;

/**
 * 币看比特币列表
 *
 * @author cimu
 * @date 2018/11/7
 */

public class BtckanDetailAdapter extends RecyclerView.Adapter<BtckanDetailAdapter.MyHolder> {
    private Context mContext;
    private List<BtckanDetailBean> mList;

    public BtckanDetailAdapter(Context context, List<BtckanDetailBean> list) {
        super();
        this.mContext = context;
        this.mList = list;

    }

    @Override
    public void onBindViewHolder(MyHolder holder, int position, List<Object> payloads) {
        super.onBindViewHolder(holder, position, payloads);
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public void onBindViewHolder(final MyHolder holder, int position) {
        final BtckanDetailBean itemBean = mList.get(position);
        try {

            double rmbPrice = MarketNumberUtils.getRealPrice(itemBean.getSite(), itemBean.getCurrency(), CurrencyAndBourseManager.getCurrencyUnitCode(), itemBean.getLast());
            //保留五位小数 数字最长7位 + . 就是最多八位了
            holder.tvRmbPrice.setText(BtcKanNumberFormatUtil.assignNumberFormat(rmbPrice, 7, 5) + "");


            double otherPrice = MarketNumberUtils.getRealPrice(itemBean.getSite(), itemBean.getCurrency(), itemBean.getCurrency(), itemBean.getLast());
            // holder.tvWbPrice.setText(BtcKanNumberFormatUtil.assignNumberFormat(otherPrice, 7, 6) + "");
//            holder.tvWbPrice.setText(new BigDecimal(otherPrice+"").toPlainString());
//经过比对，币看最多保留十位小数，并且四舍五入
//            holder.tvWbPrice.setText(BtcKanNumberFormatUtil.assignNumberFormat(otherPrice, 16, 10) + "");
            holder.tvWbPrice.setText(BtcKanNumberFormatUtil.formatNumberForSite(new BigDecimal(otherPrice + "").toPlainString()));


        } catch (Exception e) {
            e.printStackTrace();
        }
        holder.tvRmbPhoto.setText(CurrencyAndBourseManager.getCurrencySymbol(CurrencyAndBourseManager.getCurrencyUnitCode()));
        //名称
        holder.tvSiteName.setText(itemBean.getSiteName());
        //货币单位
        holder.tvCurrency.setText(itemBean.getCurrency());
        //设置左侧的量t
        holder.tvLeftAmount.setText(mContext.getResources().getString(R.string.kline_item_exchange_volume)+" " + MarketNumberUtils.amountConversion(itemBean.getLeftAmount()));
        //左侧下方货币单位
        holder.tvWbPhoto.setText(KLineUtil.getCurrencySymbol(itemBean.getCurrency()));

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TransactionTrendActivity.start(mContext, itemBean.getMarketId());
            }
        });
        //设置涨跌字体颜色
        //设置字体颜色
        if (itemBean.getTrendType() == 0) {
            holder.tvRmbPrice.setTextColor(JfResourceUtil.getSkinColor(R.color.chart_main_white_or_black));
            holder.tvRmbPhoto.setTextColor(JfResourceUtil.getSkinColor(R.color.chart_main_white_or_black));
            holder.tvWbPhoto.setTextColor(JfResourceUtil.getSkinColor(R.color.black_text_color));
            holder.tvWbPrice.setTextColor(JfResourceUtil.getSkinColor(R.color.black_text_color));
        } else if (itemBean.getTrendType() == 1) {
            holder.tvRmbPrice.setTextColor(JfResourceUtil.getSkinColor(R.color.increasing_color));
            holder.tvRmbPhoto.setTextColor(JfResourceUtil.getSkinColor(R.color.increasing_color));
            holder.tvWbPhoto.setTextColor(JfResourceUtil.getSkinColor(R.color.increasing_color));
            holder.tvWbPrice.setTextColor(JfResourceUtil.getSkinColor(R.color.increasing_color));
        } else if (itemBean.getTrendType() == -1) {
            holder.tvRmbPrice.setTextColor(JfResourceUtil.getSkinColor(R.color.decreasing_color));
            holder.tvRmbPhoto.setTextColor(JfResourceUtil.getSkinColor(R.color.decreasing_color));
            holder.tvWbPhoto.setTextColor(JfResourceUtil.getSkinColor(R.color.decreasing_color));
            holder.tvWbPrice.setTextColor(JfResourceUtil.getSkinColor(R.color.decreasing_color));
        }
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.assets_item_btckan_detail, parent, false);
        MyHolder myHolder = new MyHolder(view);
        return myHolder;
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class MyHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener{
        private TextView tvSiteName;
        private TextView tvCurrency;
        private TextView tvRmbPhoto;
        private TextView tvRmbPrice;
        private TextView tvLeftAmount;
        private TextView tvWbPhoto;
        private TextView tvWbPrice;


        public MyHolder(View itemView) {
            super(itemView);
            itemView.setOnCreateContextMenuListener(this);
            tvSiteName = (TextView) itemView.findViewById(R.id.tv_site_name);
            tvCurrency = (TextView) itemView.findViewById(R.id.tv_currency);
            tvRmbPhoto = (TextView) itemView.findViewById(R.id.tv_rmb_photo);
            tvRmbPrice = (TextView) itemView.findViewById(R.id.tv_rmb_price);
            tvLeftAmount = (TextView) itemView.findViewById(R.id.tv_left_amount);
            tvWbPhoto = (TextView) itemView.findViewById(R.id.tv_wb_photo);
            tvWbPrice = (TextView) itemView.findViewById(R.id.tv_wb_price);
        }

        @Override
        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
            //        getActivity().getMenuInflater().inflate(R.menu.self_select_menu, menu);
            final long id = SelfSelectUtils.getIsSelectedId(mList.get(getAdapterPosition()).getMarketId());
            if (id != -1) {
                menu.add(0, v.getId(), getAdapterPosition(), ResStringUtil.getString(R.string.chart_delete_self_selected));
            } else {
                menu.add(0, v.getId(), getAdapterPosition(), ResStringUtil.getString(R.string.chart_add_to_self_selected));
            }
        }
    }
}
